/**
 * 文件名：GlobalExceptionHandler
 * 版权：Copyright 2017-2022 CMCC All Rights Reserved.
 * 描述：
 */

package com.hand.project.exception;

import ch.qos.logback.classic.Logger;
import com.hand.project.result.ResponseResult;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * 定义全局异常捕获类【含异常消息的日志辅助记录】
 *
 * @author shuai.wang02@hand-china.com
 * @version 1.0
 * @date 2018/11/19 22:46
 */
@CrossOrigin
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static Logger LOGGER = (Logger) LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler
    public ResponseResult processException(Exception ex, HttpServletRequest request, HttpServletResponse response){
        ex.printStackTrace();

        if(ex instanceof MissingServletRequestParameterException){
            return new ResponseResult(400, ex);
        }
        if(ex instanceof NoPermissions){

            LOGGER.error("======="+ex.getMessage()+"=======");
            return new ResponseResult(401, "sorry，无权限！");

        }

        return new ResponseResult(500, ex.getMessage());

    }

}